package com.yaofeng.mybatis.modules.sys.controller;


import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.core.toolkit.Wrappers;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.yaofeng.mybatis.annotation.Log;
import com.yaofeng.mybatis.common.PageInfo;
import com.yaofeng.mybatis.common.ResponseInfo;
import com.yaofeng.mybatis.modules.sys.entity.Role;
import com.yaofeng.mybatis.modules.sys.entity.RoleMenu;
import com.yaofeng.mybatis.modules.sys.service.IRoleMenuService;
import com.yaofeng.mybatis.modules.sys.service.IRoleService;
import org.apache.commons.lang3.StringUtils;
import org.apache.shiro.authz.annotation.RequiresAuthentication;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.util.CollectionUtils;
import org.springframework.web.bind.annotation.*;

import javax.validation.Valid;
import java.util.ArrayList;
import java.util.List;
import java.util.Map;
import java.util.stream.Collectors;

/**
 * <p>
 *  前端控制器
 * </p>
 *
 * @author yaofeng
 * @since 2019-09-17
 */
@RestController
@RequestMapping("/sys/role")
public class RoleController {

    @Autowired
    private IRoleService  iRoleService;

    @Autowired
    private IRoleMenuService roleMenuService;

    /**
     * 查看用户列表
     * @param pageNum
     * @param pageSize
     * @return
     */
    @GetMapping("/list")
    @RequiresAuthentication
    @Log(value = "角色列表")
    public ResponseInfo list(@RequestParam Map<String, Object> params, @RequestParam(defaultValue = "1") Long pageNum, @RequestParam(defaultValue = "50") Long pageSize) {

        IPage page = iRoleService.page(new Page<>(pageNum, pageSize),
                Wrappers.<Role>lambdaQuery()
                        .like(StringUtils.isNotEmpty(params.get("roleName").toString()),Role::getRoleName,params.get("roleName").toString()));
        return ResponseInfo.success(new PageInfo(page));
    }


    @GetMapping("/select/list")
    @RequiresAuthentication
    @Log(value = "角色下拉列表")
    public ResponseInfo selectList() {
        List<Role> roleList  =   iRoleService.list();
        return ResponseInfo.success(roleList);
    }


    @GetMapping("/tree/list")
    @RequiresAuthentication
    @Log(value = "角色菜单下拉列表")
    public ResponseInfo selectList(@RequestParam("roleId") String roleId) {
        List<String> roleMenuIds =new ArrayList<>();
        List<RoleMenu> roleMenuList  =   roleMenuService.list(Wrappers.<RoleMenu>lambdaQuery().eq(RoleMenu::getRoleId,roleId));
        if(!CollectionUtils.isEmpty(roleMenuList)){
            roleMenuIds =  roleMenuList.stream().map(roleMenu -> roleMenu.getMenuId().toString()).collect(Collectors.toList());
        }
        return ResponseInfo.success(roleMenuIds);
    }

    /**
     * 保存角色
     * @param role
     * @return
     */
    @PostMapping("/save")
    @RequiresAuthentication
    @Log(value = "保存角色")
    public  ResponseInfo save(@RequestBody @Valid Role  role){
        iRoleService.saveRole(role);
        return ResponseInfo.success();
    }


    /**
     * 更新角色
     * @param role
     * @return
     */
    @PutMapping("/update")
    @RequiresAuthentication
    @Log(value = "更新角色")
    public  ResponseInfo update(@RequestBody @Valid  Role  role){
        iRoleService.updateRole(role);
        return ResponseInfo.success();
    }


    /**
     * 删除角色
     * @param id
     * @return
     */
    @DeleteMapping ("/delete")
    @RequiresAuthentication
    @Log(value = "删除角色")
    public  ResponseInfo delete(Long id){
        iRoleService.removeById(id);
        return ResponseInfo.success();
    }
}
